package com.hwl.demo1.factory;

import com.hwl.demo1.service.Operation;
import com.hwl.demo1.service.impl.Add;
import com.hwl.demo1.service.impl.Div;
import com.hwl.demo1.service.impl.Mul;
import com.hwl.demo1.service.impl.Sub;

import java.util.Scanner;

/**
 * 简单工厂模式
 * 计算器运算
 *
 * @author sentry
 * @since 2023-02-25
 */
public class OperationFactory {

    public static Operation createOperate(String operate) {
        Operation oper;
        switch (operate) {
            case "+":
                oper = new Add();
                break;
            case "-":
                oper = new Sub();
                break;
            case "*":
                oper = new Mul();
                break;
            case "/":
                oper = new Div();
                break;
            default:
                throw new UnsupportedOperationException("暂不支持该运算符");
        }
        return oper;
    }

    public static void main(String[] args) {
        double numberA;
        String strOperate;
        double numberB;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数字A：");
            numberA = Double.parseDouble(sc.nextLine());
            System.out.println("请选择运算符号（+、-、*、/）：");
            strOperate = sc.nextLine();
            System.out.println("请输入数字B：");
            numberB = Double.parseDouble(sc.nextLine());
        } catch (Exception e) {
            throw new RuntimeException("输入有误 " + e);
        }

        Operation operate = OperationFactory.createOperate(strOperate);
        double result = operate.getResult(numberA, numberB);
        System.out.println("结果是：" + result);
    }
}
